package com.geniusk.mqtt.strategy.impl;

import com.geniusk.mqtt.strategy.MessageStrategy;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.*;
import org.springframework.stereotype.Component;

/**
 * publish complete消息
 */
@Component
public class PublishCompleteMessageStrategy   implements MessageStrategy {
    @Override
    public void sendResponseMessage(Channel channel, MqttMessage mqttMessage) {
        MqttMessageIdVariableHeader messageIdVariableHeader = (MqttMessageIdVariableHeader) mqttMessage.variableHeader();
        //	构建返回报文
        MqttFixedHeader mqttFixedHeaderBack = new MqttFixedHeader(MqttMessageType.PUBCOMP,false, MqttQoS.AT_MOST_ONCE,false,0x02);
        MqttMessageIdVariableHeader mqttMessageIdVariableHeaderBack = MqttMessageIdVariableHeader.from(messageIdVariableHeader.messageId());
        MqttMessage publishCompleteMsg = new MqttMessage(mqttFixedHeaderBack,mqttMessageIdVariableHeaderBack);
        channel.writeAndFlush(publishCompleteMsg);

    }
}
